package com.taoyibao.hongcanting.receiver;

import android.content.Context;

import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.event.EventJPushCallBack;
import com.taoyibao.hongcanting.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * @author TangAnna
 * @description: 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播
 * @date :${DATA} 11:38
 */
public class HongJPushMessageReceiver extends JPushMessageReceiver {

    private static final String TAG = "HongJPushMessageReceiver";
    private EventJPushCallBack mPushCallBack = new EventJPushCallBack();

    /**
     * tag 增删查改的操作会在此方法中回调结果。
     *
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
        LogUtils.dNormal(TAG, jPushMessage.toString());
        sendMessage(CodeConstant.JPUSH_EVENT_TAG, jPushMessage);
    }

    /**
     * 查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果。
     *
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
        LogUtils.dNormal(TAG, jPushMessage.toString());
        sendMessage(CodeConstant.JPUSH_EVENT_TAG, jPushMessage);
    }

    /**
     * alias 相关的操作会在此方法中回调结果。
     *
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
        LogUtils.dNormal(TAG, jPushMessage.toString());
        sendMessage(CodeConstant.JPUSH_EVENT_ALIAS, jPushMessage);
    }

    /**
     * 设置手机号码会在此方法中回调结果
     *
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
        LogUtils.dNormal(TAG, jPushMessage.toString());
        sendMessage(CodeConstant.JPUSH_EVENT_MOBILE, jPushMessage);
    }

    /**
     * 发送消息
     *
     * @param type
     * @param jPushMessage
     */
    private void sendMessage(int type, JPushMessage jPushMessage) {
        mPushCallBack.mJPushMessage = jPushMessage;
        mPushCallBack.type = type;
        EventBus.getDefault().post(mPushCallBack);
    }
}
